********************************************************************************
************* Recoding of Campaign Panel ***************************************
******************************************************************************** 
			
			use "ZA6804_v6-0-0.dta", clear

			do "ZA6804_missing.do"

			
******************************** Position data *********************************

		*** Immigration of foreigners 
			* Ego
			fre kp7_1130
			gen w7_migrrestr_ego=kp7_1130
			gen w7_migrrestr_ego_st=(w7_migrrestr_ego-1)/6
			
			* CDU
			gen w7_migrrestr_cdu=kp7_1110a
			gen w7_migrrestr_cdu_st=(w7_migrrestr_cdu-1)/6
			recode kp7_1110a (.b=0 "migr. pos.: don't know") (1 2 3=1 "migr. pos.: facilitate") (4=2 "migr. pos.: neutral") (5 6 7=3 "migr. pos.: restrict"), gen(w7_migrrestr_cdu_cat)
			* CSU
			gen w7_migrrestr_csu=kp7_1110b
			gen w7_migrrestr_csu_st=(w7_migrrestr_csu-1)/6
			recode kp7_1110b (.b=0 "migr. pos.: don't know") (1 2 3=1 "migr. pos.: facilitate") (4=2 "migr. pos.: neutral") (5 6 7=3 "migr. pos.: restrict"), gen(w7_migrrestr_csu_cat)
			* SPD
			gen w7_migrrestr_spd=kp7_1110c
			gen w7_migrrestr_spd_st=(w7_migrrestr_csu-1)/6
			recode kp7_1110c (.b=0 "migr. pos.: don't know") (1 2 3=1 "migr. pos.: facilitate") (4=2 "migr. pos.: neutral") (5 6 7=3 "migr. pos.: restrict"), gen(w7_migrrestr_spd_cat)
			* FDP
			gen w7_migrrestr_fdp=kp7_1110d
			gen w7_migrrestr_fdp_st=(w7_migrrestr_fdp-1)/6
			recode kp7_1110d (.b=0 "migr. pos.: don't know") (1 2 3=1 "migr. pos.: facilitate") (4=2 "migr. pos.: neutral") (5 6 7=3 "migr. pos.: restrict"), gen(w7_migrrestr_fdp_cat)
			* GRUENE
			gen w7_migrrestr_gruene=kp7_1110e
			gen w7_migrrestr_gruene_st=(w7_migrrestr_gruene-1)/6
			recode kp7_1110e (.b=0 "migr. pos.: don't know") (1 2 3=1 "migr. pos.: facilitate") (4=2 "migr. pos.: neutral") (5 6 7=3 "migr. pos.: restrict"), gen(w7_migrrestr_gruene_cat)
			* The Left
			gen w7_migrrestr_linke=kp7_1110f
			gen w7_migrrestr_linke_st=(w7_migrrestr_linke-1)/6
			label variable w7_migrrestr_linke_st "LINKE pos. on restrict migration"
			recode kp7_1110f (.b=0 "migr. pos.: don't know") (1 2 3=1 "migr. pos.: facilitate") (4=2 "migr. pos.: neutral") (5 6 7=3 "migr. pos.: restrict"), gen(w7_migrrestr_linke_cat)
			* AfD
			gen w7_migrrestr_afd=kp7_1110i
			gen w7_migrrestr_afd_st=(w7_migrrestr_afd-1)/6
			label variable w7_migrrestr_afd_st "AfD pos. on restrict migration"
			recode kp7_1110i (.b=0 "migr. pos.: don't know") (1 2 3=1 "migr. pos.: facilitate") (4=2 "migr. pos.: neutral") (5 6 7=3 "migr. pos.: restrict"), gen(w7_migrrestr_afd_cat)
			* distance to Linke on migration
			gen w7_migrrestr_dist_linke=abs(w7_migrrestr_ego_st-w7_migrrestr_linke_st)
			label variable w7_migrrestr_dist_linke "distance to LINKE on migration"
			
		*** Taxes and social services
			* Ego
			fre kp7_1090
			gen w7_highertaxes_ego=kp7_1090
			gen w7_highertaxes_ego_st=(w7_highertaxes_ego-1)/6			
			
			* CDU
			gen w7_highertaxes_cdu=kp7_1070a
			gen w7_highertaxes_cdu_st=(w7_highertaxes_cdu-1)/6
			recode kp7_1070a (.b=0 "econ. pos.: don't know") (1 2 3=1 "econ. pos.: lower taxes") (4=2 "econ. pos.: neutral") (5 6 7=3 "econ. pos.: higher taxes"), gen(w7_highertaxes_cdu_cat)
			* CSU
			gen w7_highertaxes_csu=kp7_1070b
			gen w7_highertaxes_csu_st=(w7_highertaxes_csu-1)/6
			recode kp7_1070b (.b=0 "econ. pos.: don't know") (1 2 3=1 "econ. pos.: lower taxes") (4=2 "econ. pos.: neutral") (5 6 7=3 "econ. pos.: higher taxes"), gen(w7_highertaxes_csu_cat)
			* SPD
			gen w7_highertaxes_spd=kp7_1070c
			gen w7_highertaxes_spd_st=(w7_highertaxes_csu-1)/6
			recode kp7_1070c (.b=0 "econ. pos.: don't know") (1 2 3=1 "econ. pos.: lower taxes") (4=2 "econ. pos.: neutral") (5 6 7=3 "econ. pos.: higher taxes"), gen(w7_highertaxes_spd_cat)
			* FDP
			gen w7_highertaxes_fdp=kp2_1070d
			gen w7_highertaxes_fdp_st=(w7_highertaxes_fdp-1)/6
			recode kp7_1070d (.b=0 "econ. pos.: don't know") (1 2 3=1 "econ. pos.: lower taxes") (4=2 "econ. pos.: neutral") (5 6 7=3 "econ. pos.: higher taxes"), gen(w7_highertaxes_fdp_cat)
			* GRUENE
			gen w7_highertaxes_gruene=kp7_1070e
			gen w7_highertaxes_gruene_st=(w7_highertaxes_gruene-1)/6
			recode kp7_1070e (.b=0 "econ. pos.: don't know") (1 2 3=1 "econ. pos.: lower taxes") (4=2 "econ. pos.: neutral") (5 6 7=3 "econ. pos.: higher taxes"), gen(w7_highertaxes_gruene_cat)
			* Linke
			gen w7_highertaxes_linke=kp7_1070f
			gen w7_highertaxes_linke_st=(w7_highertaxes_linke-1)/6
			label variable w7_highertaxes_linke_st "LINKE pos. on higher taxes"
			recode kp7_1070f (.b=0 "econ. pos.: don't know") (1 2 3=1 "econ. pos.: lower taxes") (4=2 "econ. pos.: neutral") (5 6 7=3 "econ. pos.: higher taxes"), gen(w7_highertaxes_linke_cat)
			* AfD
			gen w7_highertaxes_afd=kp7_1070i  
			gen w7_highertaxes_afd_st=(w7_highertaxes_afd-1)/6
			label variable w7_highertaxes_afd_st "AfD pos. on higher taxes"
			recode kp7_1070i (.b=0 "econ. pos.: don't know") (1 2 3=1 "econ. pos.: lower taxes") (4=2 "econ. pos.: neutral") (5 6 7=3 "econ. pos.: higher taxes"), gen(w7_highertaxes_afd_cat)
			
		*** define left-authoritarians
			gen w7_leftauth=0 if w7_migrrestr_ego<. & w7_highertaxes_ego<.
			replace w7_leftauth=1 if w7_migrrestr_ego>=5 & w7_migrrestr_ego<. & w7_highertaxes_ego>=5 & w7_highertaxes_ego<.  
			label variable w7_leftauth "left-authoritarian"
			tab w7_leftauth, m
			label values w7_leftauth leftauth
			
			gen quadrant=0 if w7_migrrestr_ego==4 | w7_highertaxes_ego==4 
			replace quadrant=4 if w7_migrrestr_ego>=5 & w7_migrrestr_ego<. & w7_highertaxes_ego>=5 & w7_highertaxes_ego<.  
			replace quadrant=2 if w7_migrrestr_ego<=3 & w7_migrrestr_ego<. & w7_highertaxes_ego>=5 & w7_highertaxes_ego<.  
			replace quadrant=3 if w7_migrrestr_ego>=5 & w7_migrrestr_ego<. & w7_highertaxes_ego<=3 & w7_highertaxes_ego<.  
			replace quadrant=1 if w7_migrrestr_ego<=3 & w7_migrrestr_ego<. & w7_highertaxes_ego<=3 & w7_highertaxes_ego<.  
			label define quadrant 0 "residual (mid-point on at least one item)" 4 "left-authoritarian" 2 "left-lib." 3 "right-authoritarian" 1 "right-lib."
			label values quadrant quadrant

		

*************************** Most important problems ****************************
	
	// Generate a variable that entails the last valid answer 
	gen w7_mip = kp7_840_c1
	replace w7_mip = kp7_840_c2 if kp7_840_c2 < 5001
	replace w7_mip = kp7_840_c3 if kp7_840_c3 < 5001
	replace w7_mip = kp7_840_c4 if kp7_840_c4 < 5001
	replace w7_mip = kp7_840_c5 if kp7_840_c5 < 5001		
	
	// Most important problem Immigration = 1, 0 otherwise  
	gen w7_mip_immi = w7_mip
	replace w7_mip_immi = 0 if w7_mip_immi < 5001
	replace w7_mip_immi = 1 if w7_mip == 3411
	replace w7_mip_immi = 1 if w7_mip == 3413
	replace w7_mip_immi = 1 if w7_mip >= 3750 & w7_mip < 3760
	
	// Most important problem Economic = 1, 0 otherwise
	gen w7_mip_econ = w7_mip
	replace w7_mip_econ = 0 if w7_mip_econ < 5001
	replace w7_mip_econ = 1 if w7_mip >= 3540 & w7_mip < 3544
	replace w7_mip_econ = 1 if w7_mip == 3700
	replace w7_mip_econ = 1 if w7_mip == 3714 | w7_mip == 3715
	replace w7_mip_econ = 1 if w7_mip >= 3720 & w7_mip < 3726
	replace w7_mip_econ = 1 if w7_mip >= 3740 & w7_mip < 3747
	replace w7_mip_econ = 1 if w7_mip >= 3760 & w7_mip < 3772
	replace w7_mip_econ = 1 if w7_mip == 3781 | w7_mip == 3799
	replace w7_mip_econ = 1 if w7_mip >= 3800 & w7_mip < 3991
	replace w7_mip_econ = 1 if w7_mip == 4132
	replace w7_mip_econ = 1 if w7_mip >= 4300 & w7_mip < 4400
	replace w7_mip_econ = 1 if w7_mip == 4420 | w7_mip == 4421

	// Categorical variable
	gen w7_mip_cat=0 if w7_mip<.
	replace w7_mip_cat=1 if w7_mip_econ==1
	replace w7_mip_cat=2 if w7_mip_immi==1
	label define mip_cat 0 "MIP other" 1 "MIP economic" 2 "MIP migration"
	label values w7_mip_cat mip_cat
	label variable w7_mip_cat "most important problem"
		
				
******************************** Left-right ************************************

			recode kp7_1500 (1=1 "left") (11=11 "right"), gen(w7_leftright)
			label variable w7_leftright "left-right position"
			gen w7_leftright_st=(w7_leftright-1)/10
			label variable w7_leftright_st "left-right position"
			
			* categorical 
			recode kp7_1500 (1 2 3 4 5 = 1 "left") (6 = 2 "none (center)") (7 8 9 10 11 = 3 "right"), gen(w7_leftright_cat)

			* seperate dummies for left and right
			recode w7_leftright_cat (1=0) (2=0) (3=1), gen(w7_right)
			recode w7_leftright_cat (1=1) (2=0) (3=0), gen(w7_left)
		
			* intensity of left and intensity right identification
			recode kp7_1500 (1=1) (2=0.8) (3=0.6) (4=0.4) (5=0.2) (6 7 8 9 10 11 = 0), gen(w7_left_intensity)
			recode kp7_1500 (11=1) (10=0.8) (9=0.6) (8=0.4) (7=0.2) (6 7 8 9 10 11 = 0), gen(w7_right_intensity)
			
						
******************************** Party identification **************************

			recode kp7_2090a (1=1) (2=1) (3=1), gen(partyid)
			label define pid 1 "PI CDU/CSU" 4 "PI SPD" 5 "PI FDP" 6 "PI GRUENE" 7 "PI The Left" 322 "PI AfD" 801 "PI Sonstige" 808 "keine PI"
			label values partyid pid

			gen pid_afd=0 if partyid<.
			replace pid_afd=1 if partyid==322
			label variable pid_afd "PI AfD"

			gen pid_linke=0 if partyid<.
			replace pid_linke=1 if partyid==7
			label variable pid_linke "PI LINKE"
	
		
*************************** Party preferences and voting ***********************

		*** voting decision (recall) from wave 8
			clonevar w8_vote=kp8_200ba
			replace w8_vote=kp8_191ba if w8_vote>=. & kp8_191ba<. // replace with absentee vote by mail if applicable
			replace w8_vote=kp9_200ba if w8_vote>=. & kp9_200ba<. // replace with information from wave 9 if wave 8 is missing
			replace w8_vote=kp9_191ba if w8_vote>=. & kp9_191ba<. // replace with absentee vote by mail if applicable
			replace w8_vote=. if w8_vote==801 // set "others" to missing value 
			label variable w8_vote "party choice"
			label define KP8_200BA -99 "keine Angabe" -98 "weiss nicht" -97 "trifft nicht zu" -96 "Split" -95 "nicht teilgenommen" -94 "nicht in Auswahlgesamtheit" -93 "Interview abgebrochen" -92 "Fehler in Daten" -86 "nicht wahlberechtigt" -85 "nicht waehlen" -84 "Keine Erst-/ Zweitstimme vergeben" -83 "ungueltig waehlen" -82 "keine andere Partei waehlen" -81 "noch nicht entschieden" 1 "CDU/CSU" 2 "CDU" 3 "CSU" 4 "SPD" 5 "FDP" 6 "Greens" 7 "The Left" 120 "BIG" 149 "DIE GRAUEN" 151 "Die PARTEI" 168 "DVU" 171 "FAMILIE" 173 "FBI/Freie Waehler" 180 "FREIE WAEHLER" 206 "NPD" 209 "oedp" 214 "PBC" 215 "PIRATEN" 218 "pro Deutschland" 225 "REP" 234 "SSW" 237 "Tierschutzpartei" 322 "AfD" 323 "DIE RECHTE" 331 "LKR" 332 "DIE EINHEIT" 334 "Z.SH" 351 "Buendnis C" 801 "andere Partei" 808 "keine Partei" 809 "alle Parteien gleich gut" .a "keine Angabe" .b "weiss nicht" .c "trifft nicht zu" .d "Split" .e "nicht teilgenommen" .f "nicht in Auswahlgesamtheit" .g "Interview abgebrochen" .h "Fehler in Daten" .i "nicht wahlberechtigt" .j "nicht waehlen" .k "keine Erst-/Zweitstimme abgegeben" .l "ungueltig waehlen" .m "keine andere Partei waehlen" .n "noch nicht entschieden" .o "nicht einzuschaetzen" .p "nicht bekannt", replace
		
		*** voting intention
			clonevar w7_voteint=kp7_190ba
			replace w7_voteint=. if kp7_190ba==801 | kp7_191ba==801	// set "others" to missing value 
			label variable w7_voteint "voting intention"
		

********************************* Sociodemographics ****************************

		*** Age 
			gen age=(2017-kpx_2290) / 10
			
		*** Education
			fre kp1_2320
			recode kp1_2320 (1 2 = 1) (3 = 2) (4 5 = 3) (9 = .), gen(educat)
			label define educat ///
			1 "education low" ///
			2 "education medium"  ///
			3 "education high"
			label values educat educat
			
		*** Gender
			fre kpx_2280
			recode kpx_2280 (2 = 1) (1 = 0), gen(female)
			label variable female "female"
			
		*** Income 
			fre kp1_2591
			recode kp1_2591 (1 2 3 = 1 "<1000€") (4 5 6 = 2 "1000-2000€") (7 8 = 3 "2000-3000€") (9 = 4 "3000-4000€") (10 11 12 13 14 = 5 ">4000€"), gen(incomecat)
			clonevar income=kp1_2591
			gen income_st=(income-1)/12
			label variable income_st "income"
		
		*** Place of residence
			fre kp1_2600
			recode kp1_2600 (1 2 3 = 0 "Urban") (4 5 = 1 "Rural"), gen(rural1)
			recode kp1_2600 (1 2 = 0 "Urban") (3 4 5 = 1 "Rural"), gen(rural2)
			label variable rural2 "residence rural"
			
		*** Religious affiliation
			fre kp1_2480
			recode kp1_2480 (1 2 3 4 = 1) (5 6 9 = 0), gen(christian_d)
			label variable christian_d "christian religion"

		*** East-West dummy
			recode ostwest (0 = 1) (1 = 0), gen(east)
			label variable east "eastern Germany"
